package com.atguigu.userprofile.MysqlUtil;


import com.atguigu.userprofile.constant.ConstCode;
import com.google.common.base.CaseFormat;
import org.apache.commons.beanutils.BeanUtils;


import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class MysqlUtil {

     static String url = MyPropsUtil.get(ConstCode.MYSQL_URL);
     static String username  = MyPropsUtil.get(ConstCode.MYSQL_USERNAME);
     static String password = MyPropsUtil.get(ConstCode.MYSQL_PASSWORD);

    //定义一个方法
    public static <T> T selectone(String querysql,Class<T> returnType,Boolean isUnderScoreToCamel) throws SQLException, NoSuchFieldException, ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
        List<T> list = selectlist(querysql, returnType, isUnderScoreToCamel);
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    public static <T> List<T> selectlist(String querysql, Class<T> returnType,Boolean isUnderScoreToCamel){
        // 步骤:  注册驱动  获取连接  编写SQL 编译SQL  设置参数  执行SQL  处理结果  关闭连接
        ArrayList<T> resultList = new ArrayList<>();
        //获取连接
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username , password);
            preparedStatement = connection.prepareStatement(querysql);
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            //获取结果集的个数
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()) {
                //将结果集封装成一个T对象
                T t = returnType.newInstance();
                //获取每一个对象的名字
                for (int i = 1; i <= columnCount; i++) {
                    //获取列名
                    String columnName  = metaData.getColumnLabel(i);
                    //获取类的值
                    Object columnValue = resultSet.getObject(columnName);
                    //处理属性名;
                    String fieldName = null;
                    if (isUnderScoreToCamel) {
                        fieldName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnName);

                    } else {
                        fieldName = columnName;
                    }
                    //将列值封装到t对象对应的属性上
//                Field declaredField = t.getClass().getDeclaredField(filename);
//                declaredField.set(t,columnValue);
                    BeanUtils.copyProperty(t, fieldName, columnValue);
                }
                resultList.add(t);
            }
            return resultList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null && !resultSet.isClosed()) {
                    resultSet.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (preparedStatement != null && !preparedStatement.isClosed()) {
                    resultSet.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (connection != null && !connection.isClosed()) {
                    resultSet.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
